//
//  SKClock.swift
//  SKClock
//
//  Created by Sky-work on 2021/12/22.
//

import Foundation
import UIKit

typealias ClockProgressHandle = (_ count: TimeInterval) -> ()
typealias ClockHandle = () -> ()

public class SKClock {
    static var shared = SKClock() 
    
    private lazy var clocks = [ClockModel]()
    private var timer: Timer!
    
    private var startHandle: ClockHandle!
    private var stopHandle: ClockHandle!
    private var progressHandle: ClockProgressHandle!
    
//    private lazy var proxy = SKProxy.proxy(self)
    
    private init() {
        NotificationCenter.default.addObserver(self, selector: #selector(clockAction), name: Notification.Name("SKClockAction"), object: nil)
    }
    
    
    
    
    func add(_ model: ClockModel) {
        clocks.append(model)
    }
    
    func remove(_ model: ClockModel) {
        clocks.removeAll{ $0.ID == model.ID }
    }
    
    func modify(_ model: ClockModel) {
        
    }
    
    
    func start(_ model: ClockModel) {
        model.isActive = true
        
        // 开启定时器
        timer = Timer .scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerAction(_:)), userInfo: nil, repeats: true)
    }
    
    func stop(_ model: ClockModel) {
        model.isActive = false
        
        timer.invalidate()
        timer = nil
    }
    
    
    
    func testProxy() {
        let proxy = SKProxy.proxy(self)
        timer = Timer.scheduledTimer(timeInterval: 1, target: proxy, selector: #selector(testAction), userInfo: nil, repeats: true)
    }
    
    @objc private func testAction() {
        print("哈哈哈哈哈 _", #function, #line )
    }
}


public extension SKClock {
    @objc private func clockAction() {
        print("--clockAction--")
    }
    
    
    @objc private func timerAction(_ sender: Timer) {
        
    }
}



